package com.codemonkey.lq.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

class JarFoundTool {
	// 测试通过！！！
	public static void main(String args[]) {
		JarFoundTool findInJar = new JarFoundTool("POOL"); // 要寻找的字符串
		List<String> jarFiles = findInJar.find(
				"C:/Users/LQ/.m2/repository/org/mybatis/mybatis/3.2.2/", true);
		if (jarFiles.size() == 0) {
			System.out.println("Not Found");
		} else {
			for (int i = 0; i < jarFiles.size(); i++) {
				System.out.println(jarFiles.get(i));
			}
		}
	}

	private String condition; // 查询的条件

	private ArrayList<String> jarFiles = new ArrayList<String>();

	public JarFoundTool() {
	}

	/**
	 * @param condition : 要寻找的字符串
	 */
	public JarFoundTool(String condition) {
		this.condition = condition;
	}

	/**
	 * @param condition  要寻找的字符串
	 * @param exclude 被排除在外的
	 */
	public JarFoundTool(String condition, String exclude) {
		this.condition = condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	/**
	 * 寻找指定路径下jar包中含特定字符串的文件
	 * @param dir 将要进行查找的目录
	 * @param recurse 是否进行递归查找
	 * @return
	 */
	public List<String> find(String dir, boolean recurse) {
		searchDir(dir, recurse);
		return this.jarFiles;
	}

	public List<String> getFilenames() {
		return this.jarFiles;
	}

	protected String getClassName(ZipEntry entry) {
		StringBuffer className = new StringBuffer(entry.getName().replace("/", "."));
		return className.toString();
	}

	/**
	 * 寻找指定路径下jar包中含特定字符串的文件
	 *  From http://blog.csdn.net/hfhwfw/article/details/7266957
	 * @param dir 必须是一个目录! 
	 * @param recurse
	 */
	@SuppressWarnings("unchecked")
	protected void searchDir(String dir, boolean recurse) {
		try {
			File d = new File(dir);
			if (!d.isDirectory()) {
				return;
			}
			File[] files = d.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (recurse && files[i].isDirectory()) {
					searchDir(files[i].getAbsolutePath(), true);
				} else {
					String filename = files[i].getAbsolutePath();
					if (filename.endsWith(".jar") || filename.endsWith(".zip")) {
						ZipFile zip = new ZipFile(filename);
						Enumeration entries = zip.entries();
						while (entries.hasMoreElements()) {
							ZipEntry entry = (ZipEntry) entries.nextElement();
							String thisClassName = getClassName(entry);

							// 不搜索扩展名为.class的文件
							if (thisClassName.lastIndexOf(".class") == -1) {
								BufferedReader r = new BufferedReader(new InputStreamReader(zip
										.getInputStream(entry)));
								while (r.read() != -1) {
									String tempStr = r.readLine();
									if (null != tempStr && tempStr.indexOf(condition) > -1) {
										this.jarFiles.add(filename + "  --->  " + thisClassName);
										break;
									}
								}
							}

							// 获取类名为??的文件
							// if (thisClassName.equals(condition) ||
							// thisClassName.equals(condition +
							// ".class")) {
							// this.jarFiles.add(filename);
							// }
							// 获取扩展名为??的文件
							// if(thisClassName.lastIndexOf("properties")>-1) {
							// this.jarFiles.add(filename + " ---> " +
							// thisClassName);
							// }
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}